package com.shopping.servlet.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.shopping.pojo.Order;
import com.shopping.service.OrderService;
import com.shopping.service.impl.OrderServiceImpl;

/**
 * Servlet implementation class AddOrderServlet
 */
@WebServlet("/AddOrderServlet")
public class AddOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public AddOrderServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("html/text;charset=utf-8");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String content = request.getParameter("content");
		Double money = Double.valueOf(request.getParameter("money"));
		int pay = Integer.valueOf(request.getParameter("pay"));
		String dec = request.getParameter("dec");
		
		Order order = new Order(name,phone,content,money,0,pay,new Date(),dec);
		System.out.println(order);
		response.setContentType("html/text;charset=utf-8");
		PrintWriter out = response.getWriter();
		OrderService os = new OrderServiceImpl();
		boolean b = os.addOrder(order);
		JSONObject jo = new JSONObject();

		if(b==true) {
			jo.put("code", 0);
			jo.put("msg","");
		}
		else {
			jo.put("code", 1);
			jo.put("msg", "");
		}
		jo.put("count", 1);
		
		System.out.println(jo);
		out.print(jo);
	}

}
